<?php
/**
 * Created by PhpStorm.
 * User: sunqiang
 * Date: 15-3-2
 * Time: 上午11:27
 */

quanli_require('module/base/activity/activityConfig.class.php');

class activityWomensday {

    public $configName="womensday";//activities_extend表活动编号
    /**
     * 得到活动的信息
     * @return $activity_array
     * @author  2015-01-27 sun.qiang <allen.qiang@fistforward.com>
     */
    public function activity_master($pcustID){
        global $G_SHOP;


        //实例化配置类
        $activity_config = new activityConfig($this->configName); //活动配置
        $config_array = $activity_config->config;


        //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<得到活动蛋糕数量>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        $sql_master2="SELECT COUNT(1) AS item_count FROM `bill_master` WHERE billChannel = 1 and bill_status<>10 AND billPromotion=".$config_array['billPromotion'];
        $item_master = &$G_SHOP->DBCA->stmt_query($sql_master2);
        $number=$config_array['cake_sum'];
        if($item_master){
            $number =$number- $item_master[0]->item_count;
        }

        if($number<0)
            $number=0;


        //用户信息数组
        $activity_array = array(
            'startTime'	 =>  $config_array['startTime'],//活动开始时间
            'endTime'	     => $config_array['endTime'],//活动结束时间
            'activity_num' => $number,//蛋糕所剩数量
            'send_start_time' => $config_array["send_start_time"], //配送开始日期
            'send_end_time' => $config_array["send_end_time"], //配送结束日期
            'cityID'        => $config_array["cityID"], //活动城市ID
            'paytypeID' =>$config_array["paytypeID"], //活动在线支付paytypeID
            'billPromotion' => $config_array['billPromotion']
        );

        return $activity_array;

    }


    public function buy_count($pcustID,$billPromotion)
    {
        global $G_SHOP;


        //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<得到活动蛋糕数量>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        $sql_master2="SELECT COUNT(1) AS item_count FROM `bill_master` WHERE bill_status<>10 and billChannel = 1 AND pcustID = ?  AND billPromotion= ? and createTime>= ? and createTime<= ?";
        $item_master = &$G_SHOP->DBCA->stmt_query($sql_master2,'iiss',array($pcustID,$billPromotion,$G_SHOP->sys_date." 00:00:00",$G_SHOP->sys_date." 23:59:59"));
        $count = 0;
        if($item_master){
            $count = $item_master[0]->item_count;
        }

        return $count;

    }


    /**
     * 下单成功修改活动状态
     * @author  2015-01-28 allen.qiang <allen.qiang@fistforward.com>
     */
    public  function update_status($arg_array) {
        global $G_SHOP;
        //会员ID
        $pcustID = $G_SHOP->currentPCustID;

        //修改活动状态
        $activities_extend = new entity('activities_extend');
        $activities_extend->activityNumber = $this->configName; //活动编号
        $activities_extend->classifyID = 0; //0：未下单，已填写情书；1：已提交订单
        $activities_extend->extend_1 = $pcustID; //会员ID
        if($G_SHOP->DBCA->fetch($activities_extend) ) {
            $activities_extend->classifyID = 1;
            $activities_extend->extend_3 = date('Y-m-d H:i:s'); //下单时间
            $activities_extend->extend_5 = $arg_array["bill_code"];//订单编号
            $activities_extend->extend_6 = $arg_array["bill_no"];//订单号
            if (!$G_SHOP->DBCA->save($activities_extend)) {
                throw new Exception('页面已过期，请刷新页面重试', 404);
            }
            return array( 'code' => 200, 'message' => '成功');
        }else{
            return array( 'code' => 400, 'message' => '失败');
        }

    }

} 